#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(900,800);

    gameWidget = new QWidget(this);
    gameWidget->move(0, 100);
    gameWidget->resize(900, 700);
    gameWidget->setStyleSheet("background-color:white;");

    rmd = new QLabel(gameWidget);
    rmd->setGeometry(0, 500, 100, 50);
    rmd->setStyleSheet("border-image: url(:/res/Ramuda.png);");

    qsrand(time(NULL));
      double a = (0 + rand() % 9000)/double(10);
      double b = 0 + rand() % 150;
     candy = new QLabel(gameWidget);
       candy->setGeometry(a,b,50, 50);
       candy->setStyleSheet("border-image: url(:/res/Candy.png);");

       QString button_style="QPushButton{background-color:rgb(152,188,228);\
       color: black;border-radius:20px;border: 2px groove gray;\
       border-style: outset;}"
       "QPushButton:hover{background-color:white; color: black;}"
       "QPushButton:pressed{background-color:#4682B4;\
       border-style: inset; }";

       startBtn = new QPushButton(this);
           startBtn->setMaximumWidth(120);
           startBtn->setText("开始");
           startBtn->setStyleSheet(button_style);
           startBtn->setFont(QFont("黑体", 18));

           acBtn=new QPushButton(this);
              acBtn->setMaximumWidth(120);
              acBtn->setText("加速");
              acBtn->setStyleSheet(button_style);
              acBtn->setFont(QFont("黑体", 18));

              dcBtn = new QPushButton(this);
              dcBtn->setMaximumWidth(120);
              dcBtn->setText("减速");
              dcBtn->setStyleSheet(button_style);
              dcBtn->setFont(QFont("黑体", 18));

              stopBtn=new QPushButton(this);
              stopBtn->setMaximumWidth(120);
              stopBtn->setText("停止");
              stopBtn->setStyleSheet(button_style);
              stopBtn->setFont(QFont("黑体", 18));

              scorelb=new QLabel(this);
              scorelb->setGeometry(740,35,70,30);
              scorelb->setText("得分:");
              scorelb->setFont(QFont("黑体",18));


              score=new QLabel(this);
              score->setGeometry(820,35,50,30);
              score->setText("0");
              score->setFont(QFont("黑体",18));

              QWidget *layoutwindow = new QWidget(this);
              layoutwindow->setGeometry(20,10,700,80);
              QHBoxLayout *layout= new QHBoxLayout;
              layout->addWidget(startBtn);
              layout->addWidget(acBtn);
              layout->addWidget(dcBtn);
              layout->addWidget(stopBtn);
              layoutwindow->setLayout(layout);

              timer=new QTimer();
                  //打开鼠标位置追踪
                  this->setMouseTracking(true);
                  gameWidget->setMouseTracking(true);
                  gameWidget->installEventFilter(this);

                  connect(startBtn, &QPushButton::clicked, this, &MainWindow::startgame);
                  connect(stopBtn, &QPushButton::clicked, this, &MainWindow::stopgame);
                  connect(acBtn,&QPushButton::clicked,this,&MainWindow::acgame);
                  connect(dcBtn, &QPushButton::clicked, this, &MainWindow::dcgame);
                  connect(timer, SIGNAL(timeout()), this, SLOT(rmdmove()));

                  iniscore=0;
                  delay=10;
                  db=2;
              }
              //固定果篮纵向位置
              void MainWindow::resizeEvent(QResizeEvent * dragwindow)
              {
                  if (dragwindow) {
                      basket->move(rmd->x(), gameWidget->height() - 50);
                  }
              }

              void MainWindow::startgame()
              {
                  timer->start(10);
              }

              void MainWindow::acgame()
              {
                  delay = delay - 2;
                  timer->setInterval(delay);
                  timer->start();
              }

              void MainWindow::dcgame()
              {
                  delay = delay + 2;
                  timer->setInterval(delay);
                  timer->start();
              }

              void MainWindow::stopgame()
              {
                  timer->stop();
              }

              //碰撞检测
              void MainWindow::basketmove()
              {
                  if (candy->pos().x()>rmd->pos().x()-candy->width() && (candy->pos().x())<(rmd->pos().x()+rmd->width()) \
                          && (candy->pos().y()+candy->height())>rmd->pos().y())
                  {
                      iniscore++;
                      score->setText(QString("%1").arg(iniscore));
                      //随机水果位置，保留1位小数
                      a = (0 + rand() % 9000)/double(10);
                      b = 0 + rand() % 150;
                      candy->move(a,b);
                  }
                  else if (candy->pos().y()>gameWidget->height())
                  {
                      a = (0 + rand() % 9000)/double(10);
                      b = 0 + rand() % 150;
                      candy->move(a,b);
                  }

                  b += db;
                  candy->move(a,b);
              }

              //鼠标移动事件
              void MainWindow::mouseMoveEvent(QMouseEvent * e)
              {
                  e->accept();
                  if (e->pos().x()>0&& e->pos().x()<800)
                  {
                      rmd->move(e->pos().x(), rmd->pos().y());
                  }
                  else if (e->pos().x() >= 850)
                  {
                      rmd->move(800, rmd->pos().y());
                  }
              }

              MainWindow::~MainWindow()
              {
                  delete ui;
              }


MainWindow::~MainWindow()
{
    delete ui;
}
